package com.ruoyi.customer.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.customer.api.domain.CustomerManagement;
import com.ruoyi.customer.domain.CustomerBind;
import com.ruoyi.customer.domain.CustomerBindVO;
import com.ruoyi.customer.domain.WxCustomerBind;
import com.ruoyi.customer.mapper.CustomerBindMapper;
import com.ruoyi.customer.mapper.CustomerManagementMapper;
import com.ruoyi.customer.service.ICustomerBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 家属绑定Service业务层处理
 * 
 * @author luogongquan
 * @date 2024-09-11
 */
@Service
public class CustomerBindServiceImpl implements ICustomerBindService 
{
    @Autowired
    private CustomerBindMapper customerBindMapper;
    @Autowired
    private CustomerManagementMapper customerManagementMapper;

    /**
     * 查询家属绑定
     * 
     * @param id 家属绑定主键
     * @return 家属绑定
     */
    @Override
    public CustomerBind selectCustomerBindById(Long id)
    {
        return customerBindMapper.selectCustomerBindById(id);
    }

    /**
     * 查询家属绑定列表
     * 
     * @param customerBind 家属绑定
     * @return 家属绑定
     */
    @Override
    public List<CustomerBind> selectCustomerBindList(CustomerBind customerBind)
    {
        return customerBindMapper.selectCustomerBindList(customerBind);
    }

    /**
     * 新增家属绑定
     * 
     * @param customerBind 家属绑定
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCustomerBind(CustomerBind customerBind)
    {
        List<Long> bindCustomerIds = customerBind.getBindCustomerIds();
        if(bindCustomerIds.isEmpty()){
            throw new ServiceException("绑定客户不能为空");
        }
        List<CustomerBind> collect = bindCustomerIds.stream().map(bindCustomerId -> {
            CustomerBind bind = new CustomerBind();
            bind.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
            bind.setBindCustomerId(bindCustomerId);
            bind.setCustomerId(customerBind.getCustomerId());
            bind.setCreateTime(DateUtils.getNowDate());
            bind.setRelationsType(customerBind.getRelationsType());
            return bind;
        }).collect(Collectors.toList());
        return customerBindMapper.batchInsertCustomerBind(collect);
    }

    /**
     * 修改家属绑定
     * 
     * @param customerBind 家属绑定
     * @return 结果
     */
    @Override
    public int updateCustomerBind(CustomerBind customerBind)
    {
        return customerBindMapper.updateCustomerBind(customerBind);
    }

    /**
     * 批量删除家属绑定
     * 
     * @param ids 需要删除的家属绑定主键
     * @return 结果
     */
    @Override
    public int deleteCustomerBindByIds(Long[] ids)
    {
        return customerBindMapper.deleteCustomerBindByIds(ids);
    }

    /**
     * 删除家属绑定信息
     * 
     * @param id 家属绑定主键
     * @return 结果
     */
    @Override
    public int deleteCustomerBindById(Long id)
    {
        return customerBindMapper.deleteCustomerBindById(id);
    }

    @Override
    public List<CustomerBindVO> selectCustomerBindList2(CustomerBindVO customerBind) {
        return customerBindMapper.selectCustomerBindList2(customerBind);
    }

    @Override
    public int insertCustomerBind4Wx(WxCustomerBind customerBind) {
        //先判断该手机号码是否已经绑定过
        CustomerManagement mine = SecurityUtils.getLoginUser().getCustomerManagement();
        if(mine.getPhoneNumber().equals(customerBind.getPhoneNumber())){
            throw new  ServiceException("绑定失败，不能绑定自己");
        }

        //通过电话号码获取被绑定人信息
        CustomerManagement customerManagement = customerManagementMapper.selectCustomerManagementByPhoneNumber(customerBind.getPhoneNumber());
        if(Objects.isNull(customerManagement)){
            throw new ServiceException("该手机号码还没注册用户");
        }
        CustomerBind bind = new CustomerBind();
        bind.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        bind.setCustomerId(customerBind.getCustomerId());
        bind.setBindCustomerId(customerManagement.getId());
        bind.setCreateTime(DateUtils.getNowDate());
        bind.setRelationsType(customerBind.getRelationsType());
        int i = 0;
        try {
            i = customerBindMapper.insertCustomerBind(bind);
        } catch (Exception e) {
            throw new ServiceException("不能重复绑定");
        }
        return i;
    }

    @Override
    public List<CustomerBindVO> selectCustomerWardList() {
        CustomerBindVO customerBindVO = new CustomerBindVO();
        customerBindVO.setBindCustomerId(SecurityUtils.getUserId());
        return customerBindMapper.selectCustomerWardList(customerBindVO);
    }
}
